//
//  XMAlbumJigsawView.m
//  XiaoMiao
//
//  Created by kinglian_iOS_000 on 2018/2/24.
//  Copyright © 2018年 smallyou. All rights reserved.
//

#import "XMAlbumJigsawView.h"
#import "XMAlbumJigsawAvatarCell.h"

@interface XMAlbumJigsawView () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, XMAlbumJigsawAvatarCellDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *albumCollView;
@property (weak, nonatomic) IBOutlet UILabel *seletedCountLabel;
@property (weak, nonatomic) IBOutlet UIButton *startJigsawButton;

@end

@implementation XMAlbumJigsawView

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.autoresizingMask = UIViewAutoresizingNone;
    
    [self.startJigsawButton addTarget:self action:@selector(startJigsawButtonDidClick) forControlEvents:UIControlEventTouchUpInside];
    
    [self setupUI];
}

- (void)setupUI {
    self.albumCollView.delegate = self;
    self.albumCollView.dataSource = self;
    [self.albumCollView registerNib:[UINib nibWithNibName:NSStringFromClass(XMAlbumJigsawAvatarCell.class) bundle:nil] forCellWithReuseIdentifier:[XMAlbumJigsawAvatarCell cellId]];
    
    
    UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
    [self.albumCollView addGestureRecognizer:longPressGesture];
}

- (void)setAlbumImageModelArrayM:(NSMutableArray<AlbumImageModel *> *)albumImageModelArrayM {
    _albumImageModelArrayM = albumImageModelArrayM;
    

    self.startJigsawButton.enabled = albumImageModelArrayM.count != 0;
    self.seletedCountLabel.text = @(albumImageModelArrayM.count).description;
    [self.albumCollView reloadData];
}

- (void)startJigsawButtonDidClick {
    if ([self.deleagte respondsToSelector:@selector(albumJigsawView:startJigsawAlbumImageModelArrayM:)]) {
        [self.deleagte albumJigsawView:self startJigsawAlbumImageModelArrayM:self.albumImageModelArrayM];
    }
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.albumImageModelArrayM.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    XMAlbumJigsawAvatarCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[XMAlbumJigsawAvatarCell cellId] forIndexPath:indexPath];
    cell.delegate = self;
    cell.albumImageType = AlbumImageTypeDeleted;
    cell.albumImageModel = self.albumImageModelArrayM[indexPath.row];
    
    
    return cell;
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat w = (kScreenWidth - (10 * 2) - (5 * 3)) * 1.0 / 4.0;
    return CGSizeMake(w, w);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 10.f;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 10.f;
}

#pragma mark - XMAlbumJigsawAvatarCellDelegate 
- (void)albumJigsawAvatarCell:(XMAlbumJigsawAvatarCell *)albumJigsawAvatarCell didDeletedAlbumImageModel:(AlbumImageModel *)model
{
    [self.albumImageModelArrayM removeObject:model];
    self.startJigsawButton.enabled = self.albumImageModelArrayM.count != 0;
    self.seletedCountLabel.text = @(self.albumImageModelArrayM.count).description;
    
    [self.albumCollView reloadData];
    
    if ([self.deleagte respondsToSelector:@selector(albumJigsawView:didDeletedAlbumImageModel:)]) {
        [self.deleagte albumJigsawView:self didDeletedAlbumImageModel:model];
    }
}


#pragma mark - 拖拽手势相关

- (void)longPressAction:(UILongPressGestureRecognizer *)longPress {
    //获取此次点击的坐标，根据坐标获取cell对应的indexPath
    CGPoint point = [longPress locationInView:self.albumCollView];
    NSIndexPath *indexPath = [self.albumCollView indexPathForItemAtPoint:point];
    //根据长按手势的状态进行处理。
    switch (longPress.state) {
        case UIGestureRecognizerStateBegan:
            //当没有点击到cell的时候不进行处理
            if (!indexPath) {
                break;
            }
            //开始移动
            [self.albumCollView beginInteractiveMovementForItemAtIndexPath:indexPath];
            break;
        case UIGestureRecognizerStateChanged:
            //移动过程中更新位置坐标
            [self.albumCollView updateInteractiveMovementTargetPosition:point];
            break;
        case UIGestureRecognizerStateEnded:
            //停止移动调用此方法
            [self.albumCollView endInteractiveMovement];
            break;
        default:
            //取消移动
            [self.albumCollView cancelInteractiveMovement];
            break;
    }
}

// 在开始移动时会调用此代理方法，
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath {
    //根据indexpath判断单元格是否可以移动，如果都可以移动，直接就返回YES ,不能移动的返回NO
    return YES;
}

// 在移动结束的时候调用此代理方法
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath {
    /**
     *sourceIndexPath 原始数据 indexpath
     * destinationIndexPath 移动到目标数据的 indexPath
     */
    
    id item = self.albumImageModelArrayM[sourceIndexPath.row];
    [self.albumImageModelArrayM removeObjectAtIndex:sourceIndexPath.row];
    [self.albumImageModelArrayM insertObject:item atIndex:destinationIndexPath.row];
    
    [collectionView reloadData];
}




#pragma mark - <#注释#>
+ (CGFloat)viewHeigt { 
    return 44 + (10 * 3) + ((kScreenWidth - (10 * 2) - (5 * 3)) / 4.0);
}

@end
